/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.artic.ogre.loader;

import com.artic.base.resourceloader.ResourceLoader;
import com.artic.base.xml.XMLJaxbReader;
import com.artic.ogre.handler.EnviromentHandler;
import com.artic.ogre.handler.nodes.NodesHandler;
import com.artic.ogre.handler.ResourceHandler;
import com.artic.ogre.handler.SceneHandler;
import com.artic.ogre.handler.SceneProcessor;
import com.artic.ogre.handler.TerrainHandler;
import com.artic.ogre.model.Scene;
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.AssetManager;
import com.jme3.asset.DesktopAssetManager;
import com.jme3.scene.Node;
import com.jme3.scene.plugins.ogre.MeshLoader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author czarnik_a
 */
public class OgreSceneLoader implements AssetLoader {

    private AssetManager assetManager;
    private Node jmeRootNode;
    private static final String ENCODING = "UTF-8";
    private static final String CONTEXT = "com.artic.ogre.model";
    private XMLJaxbReader reader;
    private static final String DOTSCENE_XSD = "dotScene.xsd";
    private SceneProcessor sceneProcessor;
    private Scene dotScene;

    /**
     * Read the Scene File and Process the OgreDotScene.
     *
     * Pattern used:
     * <img src="http://www.gliffy.com/gliffy/api/clientdiagram?did=2265627&type=PNG&size=L"/>
     * 
     */
    public Object load(AssetInfo info) throws IOException {
        assetManager = info.getManager();
        String sceneName = info.getKey().getName();
        String ext = info.getKey().getExtension();
        String folderName = info.getKey().getFolder();
        sceneName = sceneName.substring(0, sceneName.length() - ext.length() - 1);
        jmeRootNode = new Node("OgreDotScene" + sceneName);
        if (reader == null) {
            reader = new XMLJaxbReader();
        }
        InputStream xmlstream = info.openStream();
        URL schema = ResourceLoader.getResource(DOTSCENE_XSD);
        if (xmlstream != null && schema != null) {
                    JAXBContext jc;
            try {
                jc = JAXBContext.newInstance(CONTEXT);
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                dotScene = (Scene) unmarshaller.unmarshal(xmlstream);
                sceneProcess();

            } catch (JAXBException ex) {
                Logger.getLogger(OgreSceneLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        //    dotScene = (Scene) reader.readElement(CONTEXT, schema, xmlstream, ENCODING).getValue();
        }

        return jmeRootNode;
    }

    private void sceneProcess() {
        sceneProcessor = new SceneProcessor();
        sceneProcessor.addHandler(new SceneHandler());
        sceneProcessor.addHandler(new ResourceHandler());
        sceneProcessor.addHandler(new EnviromentHandler());
        sceneProcessor.addHandler(new NodesHandler());
        sceneProcessor.addHandler(new TerrainHandler());
        sceneProcessor.processHandlers(dotScene, jmeRootNode, assetManager);
    }
}
